<template>
    <section style="padding-top: 20px;">
        <el-form ref="form" :rules="rules" :model="form" label-width="120px" label-position="right" size="medium">
            <el-form-item label="产品名称" prop="product_name" required>
                <el-input placeholder="请输入产品名称" v-model="form.product_name"></el-input>
            </el-form-item>
            <el-form-item label="产品图片" prop="product_image" required>
                <el-input type="hidden" v-model="form.product_image"></el-input>
                <el-upload
                    action="admin/upload/image_upload"
                    :auto-upload="true"
                    :multiple="false"
                    :limit="1"
                    :data="{'type':'product'}"
                    list-type="picture-card"
                    :file-list="file_list"
                    :on-success="uploadSuccess"
                    :on-preview="handlePictureCardPreview"
                    :on-remove="fileRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item prop="category_id" label="所属分类">
                <el-select v-model="form.category_id" placeholder="请选择所属分类">
                    <el-option v-for="(item,index) in categories" :key="index" :label="item.text" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="command_type" label="推荐类型">
                <el-select v-model="form.command_type" placeholder="请选择推荐类型">
                    <el-option label="普通" value="normal"></el-option>
                    <el-option label="热门推荐" value="hot"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="price" label="商品售价">
                <el-input placeholder="请填写商品售价" v-model="form.price"></el-input>
            </el-form-item>
            <el-form-item prop="discount_price" label="优惠价格">
                <el-input placeholder="请填写优惠价格" v-model="form.discount_price"></el-input>
            </el-form-item>
            <el-form-item prop="coin" label="币种">
                <el-select v-model="form.coin" placeholder="请选择商品币种">
                    <el-option label="美元" value="USD"></el-option>
                    <el-option label="英镑" value="GBP"></el-option>
                    <el-option label="欧元" value="EUR"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="price_rmb" label="价格（RMB）">
                <el-input placeholder="请填写人民币价格" v-model="form.price_rmb"></el-input>
            </el-form-item>
            <el-form-item prop="description" label="商品详情">
                <el-input placeholder="请填写商品详情" v-model="form.description"></el-input>
            </el-form-item>
            <el-form-item prop="contact_wx" label="联系人微信">
                <el-input placeholder="请填写联系人微信" v-model="form.contact_wx"></el-input>
            </el-form-item>
            <el-form-item prop="sale_nums" label="已售数量">
                <el-input placeholder="请填写已售数量" v-model="form.sale_nums"></el-input>
            </el-form-item>
            <el-form-item label="购买流程">
                <quill-editor ref="text" v-model="form.purchase_notice" @change="textChange" class="myQuillEditor" :options="editorOption" />
            </el-form-item>
            <el-form-item>
                <el-button :disabled="disabled" :loading="addLoading" type="primary" @click="submitForm('form')">立即创建</el-button>
                <el-button @click="$router.push('/product')">取消</el-button>
            </el-form-item>
        </el-form>
    </section>
</template>

<script>
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'
    import { quillEditor } from 'vue-quill-editor'
    import { addProduct, removeFile, getCategory } from "../../api/api";
    export default {
        data () {
            return {
                addLoading: false,
                disabled: false,
                editorOption: {
                    theme: 'snow'
                },
                type:'add',
                file_list: [],
                rules: {
                    product_name: [
                        { required: true, message: '请输入商品名称', trigger: 'blur' },
                        { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ],
                    coin: [
                        { required: true, message: '请选择商品币种', trigger: 'change' }
                    ],
                    command_type: [
                        { required: true, message: '请选择推荐类型', trigger: 'change' }
                    ],
                    price: [
                        { required: true, message: '商品售价不能为空', trigger: 'blur'},
                    ],
                    discount_price: [
                        { required: true, message: '商品优惠价格不能为空', trigger: 'blur' }
                    ],
                    price_rmb: [
                        { required: true, message: '人民币价格', trigger: 'blur' }
                    ],
                    category_id: [
                        { required: true, message: '请选择所属分类', trigger: 'blur' }
                    ],
                    contact_wx: [
                        { required: true, message: '请填写联系人微信', trigger: 'blur' }
                    ]
                },
                dialogImageUrl: '',
                dialogVisible: false,
                form: {
                    product_name: '',
                    category_id: '',
                    product_image: '',
                    price: '',
                    discount_price: '',
                    coin: "",
                    description: "",
                    price_rmb: '',
                    contact_wx: '',
                    sale_nums: '',
                    command_type: '',
                    purchase_notice: ''
                },
                categories: [
                    {id: '', text: ''}
                ],
            }
        },
        created() {
            this.getCategory();
        },
        methods: {
            getCategory(){
                getCategory().then((res) => {
                    console.log(res);
                    this.categories = res.data.data;
                })
            },
            uploadSuccess(res){
                this.form.product_image = res.data
            },
            fileRemove(file) {
                console.log(file);
                var file_name = file.response.data;
                var data = {'file_name': file_name};
                removeFile(data).then((res) => {
                    console.log(res);
                    if(res.code == 200){
                        this.form.product_image = ''
                    }
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            textChange () {
                console.log(this.$refs.text.value)
            },
            submitForm(formName) {
                var this_ = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this_.addLoading = true;
                        addProduct(this_.form).then((res) => {
                            console.log(res.data);
                            if (res.code == 200){
                                this_.addLoading = false;
                                this_.disabled = true;
                                this.$message({
                                    message: '商品添加成功！',
                                    showClose: true,
                                    type: 'success',
                                    duration: 1200,
                                    onClose: function(){
                                        console.log('添加成功回调');
                                        this_.$router.push('/product')
                                    }
                                });
                            }else{
                                this.$message({
                                    showClose: true,
                                    message: '添加失败，请稍后重试！',
                                    type: 'error'
                                });
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        name: "product_form",
        components: {
            quillEditor
        }
    }
</script>

<style scoped>
    .main{
        width: 80%;
        /*margin-left: 5%;*/
    }
</style>
